package org.apache.cordova.toast;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;


import com.sk.app.R;
import com.sk.app.WebActivity;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * @author Administrator
 * @FileName
 * @date 2019/2/12 14:27
 */
public class MyToast extends CordovaPlugin {
    @Override
    public boolean execute(String action, String rawArgs, CallbackContext callbackContext) throws JSONException {
        return super.execute(action, rawArgs, callbackContext);
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        return super.execute(action, args, callbackContext);
    }

    @Override
    public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
        //showToast 是你在JS中调用的方法名；
        if (action.equals("showToast")){
            //这里可以实现一些你的原生逻辑功能
            String message = args.getString(0);
            Toast.makeText(cordova.getActivity(), message, Toast.LENGTH_SHORT).show();
            Log.e("js",message);
            showMess(message,callbackContext);

            cordova.getActivity().runOnUiThread(() -> {
                Intent intent = new Intent(cordova.getActivity(),
                        WebActivity.class);
                cordova.getActivity().startActivity(intent);
            });
            return true;
        }
        return false;
    }
    private void showMess(String message, CallbackContext callbackContext) {
        //h5端传给我什么参数，此处再传回去
        if (message != null && message.length() > 0) {
            callbackContext.success(message);
            Log.e("ddd","进入了");
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }

    private void showDg(String str){
        //创建对话框
        AlertDialog dialog = new AlertDialog.Builder(cordova.getActivity()).create();
        //设置对话框icon
        dialog.setIcon(R.mipmap.icon);
        //设置对话框标题
        dialog.setTitle("温馨提示");
        //设置文字显示内容
        dialog.setMessage(str);
        //分别设置三个button
        dialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定", (dialog1, which) -> {
            dialog1.dismiss();//关闭对话框
        });
//        dialog.setButton(DialogInterface.BUTTON_NEUTRAL,"点我试试", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) { }
//        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialog12, which) -> {
            dialog12.dismiss();//关闭对话框
        });
        dialog.show();//显示对话框

    }
}
